<?php 
namespace Admin\Controller;
use Think\Controller;
class sportController extends BaseController {

	/*public function sportlist(){
		$sportModel=M('sport');//表明,conf中定义前缀
		$sport=$sportModel->order('sportid asc')->select(); //desc降序 asc升序

		//dump($sport);
		$this->sport=$sport;//name属性对应的值 $this->对应值=$sport;
			
		$this->display();
	}
*/

public function trainlist(){
    $trainModel=M('train');//表明,conf中定义前缀
    $train=$trainModel->table('superadmin s, train t ,train_type p' )->where("t.spadmin_id=s.spadmin_id and  t.train_type_id=p.train_type_id")->field('s.spadmin_name,p.train_type_name,t.*')->order('train_id asc')->select(); //desc降序 asc升序

  
    $this->train=$train;//name属性对应的值 $this->对应值=$sport;
      
 $this->display();
  }



public function deltrain(){
	$trainid=$_GET['train_id'];
	$trainModel = M("train"); // 实例化sport对象
	$train=$trainModel->where("train_id=$trainid")->select();
	$result=$trainModel->where("train_id=$trainid")->delete(); // 删除sportid为5的用户数据
	//dump($sport);
		if ($train[0]['train_pic']!='nopic.jpg') {
              $picturePath='Public/Uploads/sportphoto/'.$train[0]['train_pic'];
              unlink($picturePath);
            }
		if($result){					
    		$this->success('删除成功');//设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
			} else {
   			 $this->error('删除失败'); //错误页面的默认跳转页面是返回前一页，通常不需要设置
			}



		}
	public function isfinish(){
		$trainid=$_GET['train_id'];
		$isfinished=$_GET['isfinish'];
		dump ($isfinish);
 
		$trainModel = M("train"); // 实例化sport对象
		// 要修改的数据对象属性赋值
		switch ($isfinished) {
			case '1':
				$data['isfinished'] = '0';
				break;
			default:
				$data['isfinished'] = '1';
				break;
		}
		$result=$trainModel->where("train_id=$trainid")->save($data); // 根据条件保存修改的数据

		if($result){					
    		$this->success('更改状态成功');//设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
			} else {
   			 $this->error('更改状态失败'); //错误页面的默认跳转页面是返回前一页，通常不需要设置
			}

		
	}
 


	
public function edittrain(){
		$trainid=$_GET['train_id'];
		$trainModel=M('train');//表明,conf中定义前缀
		$train= $trainModel->where("train_id=$trainid")->select();

		//dump($user);
		$this->train=$train;//name属性对应的值 $this->对应值=$user;
		$this->display();
	}
	 
	 

public function updatetrain(){

			$data=$_POST;
			$file=$_FILES;
			
			$trainid=$data['train_id'];

			$trainModel = M('train'); // 实例化sport模型
		   
		if ($file['train_pic']['size']==0) {
            //没有修改头像
            $result=$trainModel->where("train_id=$trainid")->save($data);
  
        }else{
            //修改头像
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg','PNG');// 设置附件上传类型
            $upload->rootPath  =  './';
            $upload->savePath =  'Public/Uploads/sportphoto/';// 设置附件上传目录
            $upload->autoSub = false;
            $upload->saveName = date("YmdHis").rand(100,999);//定义名字
            $info = $upload->upload();
    
            if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
             }else{// 上传成功 获取上传文件信息
				 //保存表单数据 包括附件数据
               	$data['train_pic']= $info['train_pic']['savename']; 
                $result=$trainModel->where("train_id=$trainid")->save($data);
                	if ($data['oldpic']!='nopic.jpg') {
                	$oldpicPath='Public/Uploads/sportphoto/'.$data['oldpic'];
                	unlink($oldpicPath);
               		 	}
            		}
        	}
		if($result){
       		 $this->success('修改成功！','../sport/trainlist');
     		 }
      		else{
        		$this->error('修改失败！');
      			}
	
	}



 Public function adddtrain(){
        $data=$_POST;
        $file=$_FILES;
        //dump($file['picture']['size']);
          
        //没有头像
        $trainModel = M('train'); // 实例化User模型
        $createresult=$trainModel->create();   // 根据表单提交的POST数据创建数据对象
          if ($createresult) {
            
                $trainModel->train_name = trim(I('post.name'));
                $trainModel->train_introduce = trim(I('post.introduce'));
                $trainModel->train_time = trim(I('post.time'));  
                $trainModel->tradin_address = trim(I('post.address'));
                $trainModel->train_notice = trim(I('post.notice'));
                $trainModel->isfinished = 0; // 设置默认的用户状态 0为正常状态
                 $trainModel->train_type_id= trim(I('post.classiify'));

                $trainModel->createtime =  date("Y-m-d H:i:s"); // 设置用户的创建时间
          $trainModel->spadmin_id = trim(I('post.spadmin_id'));
             if ($file['train_pic']['size']==0) {//没有头像
                        $trainModel->train_pic = "nopic.jpg"; // 用户不上传头像默认nopic.jpg
                    }else{//有头像
                            $info=$this->MyUpload();
                            $trainModel->train_pic= $info['train_pic']['savename']; // 保存上传的照片根据需要自行组装
                          }
                 $addresult=$trainModel->add(); // 写入用户数据到数据库
                     if (false !== $addresult)
                             {
                            $this->success('发布培训成功！', '../sport/trainlist');
                             }else{ 
                                     $this->error('数据错误'); 
                                   }

        }else{
                $this->error($trainModel->getError());  
              }

    
    }



   
  public function MyUpload(){
                //上传头像
              $upload = new \Think\Upload();// 实例化上传类
              $upload->maxSize  = 3145728 ;// 设置附件上传大小
              $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
              $upload->rootPath  =  './';
              $upload->savePath =  'Public/Uploads/sportphoto/';// 设置附件上传目录
              $upload->autoSub = false;
              $upload->saveName = date("YmdHis").rand(100,999);//定义名字
               // 上传单个文件 
              $info = $upload->upload();                    
                  if(!$info) {// 上传错误提示错误信息
                      $this->error($upload->getError());
                    }else{// 上传成功 获取上传文件信息
                              // dump($info);    
                             //dump($info['picture']['savename']);
                             // 保存表单数据 包括附件数据
                         //处理头像图片

                $picinfo="Public/Uploads/sportphoto/".$info['train_pic']['savename'];
                $image = new \Think\Image(); 
                $image->open($picinfo);
                // 生成一个居中裁剪为150*150的缩略图并保存
                 $image->save($picinfo);
                $image->open($picinfo)->text('AppStore','Public/assets_a/font/jinglei.ttf',20,'#000000',\Think\Image::IMAGE_WATER_SOUTHEAST)->save($picinfo); 
                return $info;
                         }
    }      


}
 ?>